import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import Dashboard from '../views/Dashboard.vue';
import SecondaryPage from '../views/SecondaryPage.vue';
import adminMent from '../views/admimMent/adminMent.vue';
import seriesMent from '@/views/seriesMent/seriesMent.vue';
import commodityMent from '@/views/commodityMent/commodityMent.vue';
import feedback from '@/views/feedback/feedback.vue';
import contentManagement from '@/views/contentManagement/contentManagement.vue';
import commentManagement from '@/views/commentManagement/commentManagement.vue';
import notificationManagement from '@/views/notificationManagement/notificationManagement.vue';
import roleManagement from '@/views/roleManagement/roleManagement.vue';
import rightsManagement from '@/views/rightsManagement/rightsManagement.vue';
import reportManagement from '@/views/reportManagement/reportManagement.vue'
import darkRoomManagement from '@/views/darkRoom/darkRoom.vue'
import Login from '@/views/Login/Login.vue'
import Layout from '../layout/layout.vue';
import NotFound from '@/views/404.vue'
// const baseUrl = process.env.BASE_URL || '/';
const baseUrl = '/';
const routes = [
    {
        path: '/',
        component: Layout,
        redirect: "/index",
        children: [
            {
                path: '',
                name: '首页',
                component: Home,
                meta: { sidebar: true, displayName: '首页', cache: true },
                icon: 'House',
            },
            {
                path: 'adminMent',
                name: '管理员管理',
                component: adminMent,
                meta: { sidebar: true, displayName: '管理员管理', cache: true },
                icon: 'UserFilled',
            },
            {
                path: 'roleManagement',
                name: '角色管理',
                component: roleManagement,
                meta: { sidebar: true, displayName: '角色管理', cache: true },
                icon: 'Avatar',
            },
            {
                path: 'rightsManagement',
                name: '权限管理',
                component: rightsManagement,
                meta: { sidebar: true, displayName: '权限管理', cache: true },
                icon: 'Lock',
            },
            {
                path: 'dashboard',
                name: '用户管理',
                component: Dashboard,
                meta: { sidebar: true, displayName: '用户管理', cache: true },
                icon: 'User',
            },
            {
                path: 'seriesMent',
                name: '产品管理',
                component: seriesMent,
                hidden: false,
                meta: { sidebar: true, displayName: '产品管理' },
                icon: 'Memo',
            },
            {
                path: 'commodityMent',
                name: '款式管理',
                component: commodityMent,
                hidden: false,
                meta: { sidebar: true, displayName: '款式管理' },
                icon: 'Goods',
            },
            {
                path: 'feedback',
                name: '反馈管理',
                component: feedback,
                hidden: false,
                meta: { sidebar: true, displayName: '反馈管理' },
                icon: 'ChatDotSquare',
            },
            {
                path: 'contentManagement',
                name: '内容管理',
                component: contentManagement,
                hidden: false,
                meta: { sidebar: true, displayName: '内容管理' },
                icon: 'Tickets',
            },
            {
                path: 'commentManagement',
                name: '评论管理',
                component: commentManagement,
                hidden: false,
                meta: { sidebar: true, displayName: '评论管理' },
                icon: 'ChatRound',
            },
            {
                path: 'notificationManagement',
                name: '通知管理',
                component: notificationManagement,
                hidden: false,
                meta: { sidebar: true, displayName: '通知管理' },
                icon: 'ChatRound',
            },
            {
                path: 'reportManagement',
                name: '举报管理',
                component: reportManagement,
                hidden: false,
                meta: { sidebar: true, displayName: '举报管理' },
                icon: 'Warning',
            },
            {
                path: 'darkRoomManagement',
                name: '小黑屋',
                component: darkRoomManagement,
                hidden: false,
                meta: { sidebar: true, displayName: '小黑屋' },
                icon: 'Mute',
            },
            // {
            //     path: '',
            //     name: '仪表盘',
            //     component: '',
            //     hidden: false,
            //     meta: { sidebar: true, displayName: '仪表盘' },
            //     icon: 'Odometer',
            //     children: [
            //         {
            //             path: 'dashboard',
            //             name: '表格',
            //             component: Dashboard,
            //             hidden: false,
            //             meta: { sidebar: true, displayName: '表格' },
            //             icon: 'Odometer',
            //         },
            //         {
            //             path: 'secondaryPage',
            //             name: '商品表格',
            //             component: SecondaryPage,
            //             hidden: false,
            //             meta: { sidebar: true, displayName: '商品表格' },
            //             icon: 'House',
            //         },
            //     ]
            // },
        ]

        // children: [
        //     {
        //         path: '',
        //         name: 'Home',
        //         component: Home,
        //         meta: { sidebar: true, displayName: '首页' },
        //         icon: 'House',

        //         children: [
        //             {
        //                 path: 'dashboard',
        //                 name: 'Dashboard',
        //                 component: Dashboard,
        //                 meta: { sidebar: true, displayName: '仪表盘' },
        //                 icon: 'Odometer',
        //             },
        //         ]
        //     },
        //     {
        //         path: 'dashboard',
        //         name: 'Dashboard',
        //         component: Dashboard,
        //         meta: { sidebar: true, displayName: '仪表盘' },
        //         icon: 'Odometer',
        //         children: [
        //             // {
        //             //     path: 'dashboard',
        //             //     name: 'Dashboard',
        //             //     component: Dashboard,
        //             //     meta: { sidebar: true, displayName: '仪表盘' },
        //             //     icon: 'Odometer',
        //             // },
        //             {
        //                 path: 'secondaryPage',
        //                 name: 'SecondaryPage',
        //                 component: SecondaryPage,
        //                 meta: { sidebar: true, displayName: '副页' },
        //                 icon: 'House',
        //             },
        //         ]
        //     },
        //     // {
        //     //     path: 'settings',
        //     //     name: 'Settings',
        //     //     component: Settings,
        //     //     meta: { sidebar: true, displayName: '设置' },
        //     // },
        //     // 其他路由配置
        // ],
    },
    // 其他不需要 Layout 布局的页面路由
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { sidebar: true, displayName: '登录' },
    },
    { path: '/:pathMatch(.*)', component: NotFound },
];
const router = createRouter({
    // history: createWebHistory(baseUrl === '/' ? '/' : baseUrl),
    history: createWebHistory(),
    routes,
});

// let OneHour = 86400000 * 7  // 7天
router.beforeEach((to, from, next) => {
    sessionStorage.setItem('name', to.name)// 用sessionStorage储存路由高亮
    let token = localStorage.getItem("token");
    // let dateTime = Number(localStorage.getItem("dateTime"));
    try {
        token = JSON.parse(token);
    } catch (err) { }
    // let date = new Date().getTime();
    if (!token && to.path !== '/login') {
        next('/login'); // 重定向到登录页面
    } else {
        // if (date - OneHour >= dateTime && to.path !== '/login') {
        //     localStorage.removeItem("token");
        //     localStorage.removeItem("dateTime");
        //     localStorage.removeItem("userName");
        //     ElMessage({
        //         type: 'success',
        //         message: '您的登录已过期，请重新登录',
        //     })
        //     next('/login');
        // } else {
        //     next(); // 允许导航继续
        // }
        next();
    }

    /* 路由发生变化修改页面title */
    if (to.meta.displayName) {
        document.title = to.meta.displayName;
    }
});
export default router;